package org.lq.shopping.dao.impl;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.ShopUserDao;
import org.lq.shopping.entity.ShopUser;
import org.lq.shopping.util.DruidUtil;

public class ShopUserDaoImpl implements ShopUserDao {
	Logger log = Logger.getLogger(ShopUserDaoImpl.class);
	QueryRunner qr=new QueryRunner(DruidUtil.getDataSource());


	@Override
	public int delete(int id) {
		log.info("这是一个删除操作");
		int num = 0;
		String sql = "delete from shop_user where user_id = ?";
		try {
			num = qr.execute(sql, id);
			log.debug("sql语句："+sql);
			log.debug("传入的参数值："+id);
			
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		
		return num;
	}

	@Override
	public int update(ShopUser t) {
		log.info("这是一个修改操作");
		int num = 0;
		String sql = "update shop_user" +
						"   set" + 
						"       uname = ?," + 
						"       nickname = ?," + 
						"       upass = ?," + 
						"       phone = ?," + 
						"       gender = ?," + 
						"       age = ?," + 
						"       img = ?," + 
						"       income = ?," + 
						"       education = ?," + 
						"       occupation = ?," + 
						"       name = ?," + 
						"       member = ?," + 
						"       integral = ?," + 
						"       regtime = ?," + 
						"       vip_id = ?," + 
						"       email = ?," + 
						"USER_STATE=?"+
						" where user_id = ?";
		try {
			num = qr.execute(sql, t.getUname(),t.getNickname(),t.getUpass(),t.getPhone(),t.getGender(),t.getAge(),t.getImg(),t.getIncome(),t.getEducation(),t.getOccupation(),t.getName(),t.getMember(),t.getIntegral(),t.getRegtime(),t.getVip_id(),t.getEmail(),t.getUser_state(),t.getUser_id());
			log.debug("sql语句："+sql);
			log.debug("传入的参数值："+t.toString());
		} catch (SQLException e) {
			log.error(e.toString());
		}

		return num;
	}

	@Override
	public List<ShopUser> findAll() {
		log.info("这是一个查询所有的操作");
		List<ShopUser> lists = null;
		String sql = "select *  from shop_user ";
		try {
			lists = qr.query(sql, new BeanListHandler<ShopUser>(ShopUser.class));
			log.debug("sql语句："+sql);
			
		} catch (SQLException e) {
			log.error(e.toString());
		}
		return lists;
	}

	@Override
	public List<ShopUser> LikeColumnByValue(String columName, Object value) {
		log.info("这是一个根据列名查询操作");
		String sql = "select * from shop_user where "+columName+" like "+value;
		List<ShopUser> lists = null;
		try {
			lists = qr.query(sql, new BeanListHandler<>(ShopUser.class));
			log.debug("sql语句："+sql);
			log.debug("传入的参数值："+columName+"\t"+value);
		} catch (SQLException e) {
			log.error(e.toString());
		}
		return lists;
	}

	@Override
	public int getCount() {
		log.info("这是一个查询 总行数的方法");
		int count=0;
		String sql="select count(1) from  shop_user ";
		try {
			count=qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			log.error(e.toString());
		}
		log.debug("sql语句："+sql);
		return count;
	}

	@Override
	public List<ShopUser> findPage(int pageIndex, int pageSize) {
		log.info("这是一个分页");
		String sql = "select * from("
				+ " select rownum r,t.* from ("
				+ " select * from shop_user order by user_id "
				+ ") t where rownum <= ? "
				+ ") where r > ?";
		//最大上限
		int max = pageIndex * pageSize;
		//最小下限
		int min = (pageIndex-1)*pageSize;
		List<ShopUser> list = null;
		try {
			list = qr.query(sql, new BeanListHandler<>(ShopUser.class),max,min);
			log.debug("sql语句："+sql);
		} catch (SQLException e) {
			log.error(e.toString());
		}
		return list;

	}

	@Override
	public ShopUser Login(String account, String upass) {
		log.info("根据账户进行登录");
		String   sql="select * from shop_user where uname=? and upass=?  ";
		ShopUser user=null;
		try {
			user=qr.query(sql, new BeanHandler<>(ShopUser.class),account,upass);
			log.debug("sql语句："+sql);
		} catch (SQLException e) {
			log.error(e.toString());
		}
		
	return user;
	}

	@Override
	public ShopUser findByUname(String uname) {
		log.info("根据账户名称返回用户对象");
		String   sql="select * from shop_user where uname=?  ";
		ShopUser user=null;
		try {
			user=qr.query(sql, new BeanHandler<>(ShopUser.class),uname);
			log.debug("sql语句："+sql);
		} catch (SQLException e) {
			log.error(e.toString());
		}
		
	return user;
	}

	@Override
	public ShopUser findById(int id) {
		log.info("根据账户id返回用户对象");
		String   sql="select * from shop_user where user_id=?  ";
		ShopUser user=null;
		try {
			user=qr.query(sql, new BeanHandler<>(ShopUser.class),id);
			log.debug("sql语句："+sql);
		} catch (SQLException e) {
			log.error(e.toString());
		}
		
	return user;
	}

	@Override
	public int add(ShopUser t) {
		// TODO Auto-generated method stub
		return 0;
	}

}
